// login.js
var app = getApp();
var md5 = require('../../utils/md5.js')
var util = require('../../utils/util.js')

Page({
  data: {
    phone:null,
    pwd:null,
  },
  onLoad: function (options) {

  },
  
  bindinput_phone:function(e){
    console.log('phone=' + e.detail.value);
    this.setData({ phone:e.detail.value});
  },
  bindinput_pwd:function(e){
    console.log('pwd=' + e.detail.value);
    this.setData({ pwd: e.detail.value });
  },
  formSubmit: function (e) {  // 登录
    var v = e.detail.value;
    v['pwd'] = md5.hexMD5(v.pwd)
    console.log('form submit & data=', v)
    if (util.checkRegister(v)){
      wx.request({
        url: app.globalData.query_ip + '/wx_login',
        method: 'POST',
        data: {
          data: JSON.stringify(v),
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: function (res) {
          if (res.data.openid) {
            var openid = res.data.openid
            app.globalData.openid = openid;
            app.globalData.userName = res.data.username;
            wx.switchTab({
              url: '../index/index',
            });
          } else {
            wx.showToast({
              title: '账户或密码错误!',
              icon: 'none',
              duration: 2000
            });
          }
        },
      });
    }  
  }, 

  formReset: function () {
    console.log('form reset')
  },
  goto_register:function(){
    wx.navigateTo({
      url: '../register/register',
    })
  },
  goto_index: function () {
    wx.switchTab({
      url: '../index/index',
    });
  },
  
})